package com.weixiao.controller;

import com.weixiao.model.cmn.Dict;
import com.weixiao.result.Result;
import com.weixiao.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * author 韩诚
 * date 2022/10/27 10:58
 */
//@CrossOrigin 解决跨域问题，使用getway网关之后需要注释掉
@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    /**
     * 根据数据id查询子数据列表
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> listdict = dictService.findChildData(id);
        return Result.ok(listdict);
    }

    /**
     * 导出（下载）
     * @param response
     */
    @ApiOperation(value = "导出")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @ApiOperation(value = "导入")
    @PostMapping("/importData")
    public Result importData(MultipartFile file) {
        dictService.importData(file);
        return Result.ok();
    }

    /**
     * 根据DictCode和value获取数据字典名称
     * @param dictCode
     * @param value
     * @return
     */
    @ApiOperation(value = "根据DictCode和value获取数据字典名称")
    @GetMapping(value = "/getName/{dictCode}/{value}")
    public String getName(
            @ApiParam(name = "parentDictCode", value = "上级编码", required = true)
            @PathVariable String dictCode,
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable String value) {
        return dictService.getDictName(dictCode, value);
    }

    /**
     * 根据value获取数据字典名称
     * @param value
     * @return
     */
    @ApiOperation(value = "根据value获取数据字典名称")
    @ApiImplicitParam(name = "value", value = "值", required = true, dataType = "Long", paramType = "path")
    @GetMapping(value = "/getName/{value}")
    public String getName(
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable String value) {
        return dictService.getDictName("", value);
    }

    /**
     * 根据dictCode获取下级节点
     * @param dictCode
     * @return
     */
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }
}